package applet;
import processing.core.PApplet;
import processing.core.PImage;

class Button extends GuiElement
{
	public PImage icon;
	public  String label;
	public  int fontSize;
	public boolean color = false;
	public boolean fixedSize = true;//false;	

	public Button()
	{
		fontSize = 16;
		height = 20;
	}

	public void setIcon(String name)
	{
		if(name != null)
		{
			icon = GLOBAL.processing.loadImage(name);
			if(!fixedSize)
				width = width + 18;
		}
	}

	public void setLabel(String name)
	{
		label = name;
		GLOBAL.processing.textFont(GLOBAL.tFont, fontSize);
		if(fixedSize)
		{
			width = GLOBAL.defaultMenuWidth;
			if(GLOBAL.processing.textWidth(name)>width)
				fontSize = (int) (2 * width / name.length()) -6;
			if(fontSize<=13)
				fontSize = 14;
			//GLOBAL.processing.println("name "+ name + " " +GLOBAL.processing.textWidth(name)+ " "+ name.length() + " "+fontSize);
		}
		else
		{
			width += (int)(GLOBAL.processing.textWidth(name) + 10);
			if(width == 10)
				width = 0;
		}
		
	}

	@Override
	public void draw()
	{

		int cx = x;
		int cy = y;
		
		

		if(mouseOver()) //draw the menu of sub-categories
		{
			if(GLOBAL.processing.mousePressed)
			{
				GLOBAL.processing.fill(GLOBAL.colorButtonActive);
				GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255);
				GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);
				cx += 2;
				cy += 2;
			}
			else
			{
				GLOBAL.processing.fill(GLOBAL.colorButtonBackground);
				GLOBAL.processing.rectMode(PApplet.CORNER);
				GLOBAL.processing.rect(x, y, width, height);
				GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(170, 150, 200), 255);
				GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(170, 150, 200), 150);
			}
				
		}
		else
		{
			super.draw();
		}
			
		if(label != null)
		{
			//GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
			GLOBAL.processing.strokeWeight(1);
			GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
			GLOBAL.processing.textAlign(PApplet.CENTER, PApplet.CENTER);
			GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
			if(icon!=null)
				GLOBAL.processing.text(label, cx + (width/2) + 10, cy + (height/2));
			else
			{
				if(height == 38) //ageButtons
				{
					GLOBAL.processing.textFont(GLOBAL.tFont32);
					GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
				}
				else
				{
					GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
					GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
				}
			}
				
		}

		if(icon != null)
		{
			GLOBAL.processing.imageMode(PApplet.CENTER);
			if(width == 18)
				GLOBAL.processing.image(icon, x+width/2, y+height/2, 14+(cx-x),14+(cy-y));
			else if(width == 190) //sexButton
				GLOBAL.processing.image(icon, x+width/2, y+height/2, 0.8f*(width-(cx-x)-5),0.8f*(width-(cy-y)-5));
			else
				GLOBAL.processing.image(icon, x+5, y+5, 16,16);
			
		}
	}

	public void setFontSize(int size) {
		fontSize = size;
	}

	public void doAction() {
		System.out.println("You have selected " + label);
	}

}